package local.blueheart.data.springdatademo.service;

import local.blueheart.data.springdatademo.domain.system.AppUser;
import local.blueheart.data.springdatademo.repository.UserRepository;
import local.blueheart.data.springdatademo.security.AppUsrePrincipal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class AppUserDetailService implements UserDetailsService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        final AppUser appUser = userRepository.findByUsername(username);
        if(appUser == null){
            throw  new UsernameNotFoundException(username);
        }
        //正常返回一个UserDetail实现类
        return new AppUsrePrincipal(appUser);
    }
}
